/* ------------------------------------------------------------
 * Bunny project - Data entry UI
 * UI
 * ------------------------------------------------------------ 
 */

final int		ENTRY_BOX_WIDTH		= 300;
final int		ENTRY_BOX_HEIGHT		= 140;
final color		ENTRY_BOX_COLOR		= color(0, 0, 0, 255);

final int		TEXT_FIELD_WIDTH		= 60;
final int		TEXT_FIELD_HEIGHT	= 35;

class EntryBox
{	
	public EntryBox() 
	{
		visible = false;
		txt = "";
		instructionFont = createFont("Helvetica", 14.0);
		entryFont = createFont("Helvetica", 25.0);
	}
	
	public boolean isVisible() { return visible; }
	public void hide() { visible = false; }
	
	public boolean keyPressed()
	{
		if (!visible)
		{
			return false;
		}
		
		if (key == BACKSPACE && txt.length() > 0)
		{
			txt = txt.substring(0, txt.length()-1);
		}
		else if (key == ENTER && txt.length() > 0)
		{
			// enter left over amount
			ArrayList<Float> cell = theData.getCell(entryRow, entryCol);
			cell.add( (float) (Integer.parseInt(txt)) );
			theData.saveFile(DATA_FILENAME);
			visible = false;
			
			// if send file over network
			if (SEND_UPDATES_TO.length() > 0)
			{
				theData.sendToNetwork( myApp, SEND_UPDATES_TO );
			}
		}
		else if (txt.length() < 3)
		{
			if (key >= (int) '0' && key <= (int) '9')
			{
				txt += key;
			}
		}
		return true;
	}
	
	void render()
	{
		if (!visible)
		{
			return;
		}
		
		// render overdrop to decrease visibility of other elements
		pushStyle();
		{
			fill(0,0,0,190);
			noStroke();
			rectMode(CORNER);
			rect(0,0,width,height);
		}
		popStyle();
		
		pushStyle();
		{
			// render box
			rectMode(CENTER);
			fill(ENTRY_BOX_COLOR);
			noStroke();
			rect(width/2,height/2, ENTRY_BOX_WIDTH, ENTRY_BOX_HEIGHT);
			
			// render instructions
			textFont(instructionFont);
			textAlign(LEFT, TOP);
			fill(255);
			text("Enter Food Left-over amount: ", 
				width/2 - ENTRY_BOX_WIDTH/2 + 10,
				height/2 - ENTRY_BOX_HEIGHT/2 + 10
			);

			
			// render the text field
			rectMode(CORNER);
			stroke(255);
			strokeWeight(2);
			fill(0);
			
			int xTxt = width /2 - ENTRY_BOX_WIDTH/2 + 30;
			int yTxt = height/2 + ENTRY_BOX_HEIGHT/2 - 35 - 20;
			
			rect( xTxt, yTxt, TEXT_FIELD_WIDTH, TEXT_FIELD_HEIGHT );
			
			// render the actual text
			fill(255);
			textFont(entryFont);
			text("%", xTxt - 25, yTxt + 5);
			text(txt, xTxt + 5, yTxt + 5);
			
			
		}
		popStyle();
	}
	
	public void setEntryIndex(int row, int col)
	{
		entryRow = row;
		entryCol = col;
		visible = true;
		txt = "";
	}
	
	private boolean visible;
	private String txt;
	private PFont entryFont;
	private PFont instructionFont;
	
	private int entryRow, entryCol;
	
}

